﻿using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class XSPDC : PDCBase
    {
        public ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        public BindingListCollectionView cvNOrder
        {
            get
            {
                DataGrid dlg = this.FindName("dlgNOrders") as DataGrid;
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlg.ItemsSource);
            }
        }
        public CollectionViewSource vwTPDC
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTPDC");
            }
        }
        public DataRow dwTPDC
        {
            get
            {
                if (vwTPDC.View == null)
                    return null;
                if ((DataRowView)vwTPDC.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwTPDC.View.CurrentItem).Row;
            }
        }
        public CollectionViewSource vwPDC
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwPDC");
            }
        }
        public DataRow dwPDC
        {
            get
            {
                if (vwPDC.View == null)
                    return null;
                if ((DataRowView)vwPDC.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwPDC.View.CurrentItem).Row;
            }
        }
        ITXEXRPT ITXEXRPT;
        public XSPDC()
        {
            InitializeComponent();
            ITXEXRPT = new ITXEXRPT();
            ITXEXRPT.Root = "C:\\IntexReport";
            ITXEXRPT.strCompany = app.company;
            ITXEXRPT.PrintType = "OpenExcel";

        }
        /// <summary>
        /// SetProd 当前订单
        /// SetData保存所有订单，但是用于展示今日订单
        /// SetEnv保存所有订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                IsLoaded = true;
                if (vm == null)
                {
                    vm = this.DataContext as MainViewModel;
                }

                if (vm.app.User.Trim() == "ITXV")
                {
                    vm.SPINLOT = "HSP";//140161016";  
                    /*
                    vm.KRS = "XS";
                    vm.LFD= "000032";
                    vm.POS = "001"; 
                    vm.DATES = "20141010";
                    vm.DATET = "20151020"; */
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public virtual void btnFilter_Click(object sender, RoutedEventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }

        }
        public virtual void btnFilterClear_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                cvNOrder.CustomFilter = null;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }
        TextBox tb = null;
        public void Load_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                DataRow dwData = SetEnv.ICC_PDC.NewRow();
                dwData["ABFAGANG"] = vm.ABFAGANG;
                dwData["NEWDATE"] = vm.LMONTH;
                vm.BuildData(dwData);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ITXEXRPT.dsSource = SetData;
                ITXEXRPT.dsSource.Merge(SetEnv);
                ITXEXRPT.DocType = "YGRHRPDCXS";
                ITXEXRPT.strExcelFile = @"YGR\YGRHRPDC.xls";
                string filename = ITXEXRPT.WriteExcelReport();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void ShowDetail_Click(object sender, RoutedEventArgs e)
        {
            if (dwPDC == null)
                return;
            DataRow dwData = SetEnv.ICC_PDC.NewRow();
            dwData["ABFAGANG"] = vm.ABFAGANG;
            dwData["NEWDATE"] = vm.LMONTH;
            dwData["PDCSTAFF"] = dwPDC["HRITPNR"];
            SetProd.Clear();
            DataSet SResult = INTEXDBC.QueryICC_PDC(dwData, 4);

            SetProd.Merge(SResult);
        }
    }
}
